package com.rykj.qxj.test.jdbcTemplate;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JDBCTemplate {
	
	public static void main(String[] args) {
		
		DriverManagerDataSource dataSource=new DriverManagerDataSource();
	      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
	      dataSource.setUrl("jdbc:mysql://localhost:3306/test");
	      dataSource.setUsername("root");
	      dataSource.setPassword("123");
	      
	      JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
	      NamedParameterJdbcTemplate namedParameterJdbcTemplate=new NamedParameterJdbcTemplate(jdbcTemplate);
	      
	      System.out.println("========================分页查询测试开始=============================");
	      String sql="select * from student where name=:name";
	      
	      Map<String,Object> paramMap=new HashMap<String, Object>();
	      paramMap.put("name", "张三");
	      List<Student> query = namedParameterJdbcTemplate.query(sql, paramMap, new BeanPropertyRowMapper<Student>(Student.class));
	      for (Student student : query) {
	    	 System.out.println(student.getId()+"  "+student.getName()+" "+student.getAge()+" "+student.getGradeId());
			
		}
	      List<Object> query1 = namedParameterJdbcTemplate.query(sql, paramMap, new BeanPropertyRowMapper<Object>(Object.class));
	      
	      List<Map<String, Object>> queryForList = namedParameterJdbcTemplate.queryForList(sql, paramMap);
	      
	      System.out.println(queryForList);
	      System.out.println("=================分页查询测试结束==========================");
	}
}
